package com.qztc.news_end_customer.controller;

import com.qztc.news_end_customer.config.GlobalConfig;
import com.qztc.news_end_dto.dto.Admin;
import com.qztc.news_end_dto.dto.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/thenews/user")
public class UserControllerByRestTemplate extends GlobalConfig {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/get")
    public String get() {
        return restTemplate.getForObject(SEVICE_URL+"user/get", String.class);
    }

    @DeleteMapping("/deletebyuserid")
    public String deletebyuserid(@RequestBody User user) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<User> httpEntity = new HttpEntity<>(user,headers);
        ResponseEntity<String> result = restTemplate.exchange(SEVICE_URL+"user/deletebyuserid", HttpMethod.DELETE,httpEntity,String.class);
        return result.getBody();
    }

    @PutMapping("/update_isuse_0")
    public String update_isuse_0(@RequestBody User user) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<User> httpEntity = new HttpEntity<>(user,headers);
        ResponseEntity<String> result = restTemplate.exchange(SEVICE_URL+"user/update_isuse_0", HttpMethod.PUT,httpEntity,String.class);
        return result.getBody();
    }

    @PutMapping("/update_isuse_1")
    public String update_isuse_1(@RequestBody User user) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<User> httpEntity = new HttpEntity<>(user,headers);
        ResponseEntity<String> result = restTemplate.exchange(SEVICE_URL+"user/update_isuse_1", HttpMethod.PUT,httpEntity,String.class);
        return result.getBody();
    }

    @PostMapping("/register")
    public String register(@RequestBody User user) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        user.setIsuse(1);
        HttpEntity<User> httpEntity = new HttpEntity<>(user,headers);
        ResponseEntity<String> result = restTemplate.exchange(SEVICE_URL+"user/register", HttpMethod.POST,httpEntity,String.class);
        return result.getBody();
    }

    @GetMapping("/getbyuserid")
    public String getbyuserio(@RequestParam Integer userid) {
        return restTemplate.getForObject(SEVICE_URL+"user/getbyuserid?userid=" + userid, String.class);
    }

    @PostMapping("/login")
    public String login(@RequestBody User user) {
        HttpEntity<User> httpEntity = new HttpEntity<>(user);
        ResponseEntity<String> result = restTemplate.exchange(SEVICE_URL+"user/login", HttpMethod.POST,httpEntity,String.class);
        return result.getBody();
    }

    @PutMapping("/update")
    public String update(@RequestBody User user) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<User> httpEntity = new HttpEntity<>(user,headers);
        ResponseEntity<String> result = restTemplate.exchange(SEVICE_URL+"user/update", HttpMethod.PUT,httpEntity,String.class);
        return result.getBody();
    }
}
